// (c) Copyright Mikhail Dikov. All other rights reserved.

/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference path="../ExtenderBase/BaseScripts.js" />
/// <reference path="../Compat/Timer/Timer.js" />
/// <reference path="../Common/Common.js" />
/// <reference path="../Animation/Animations.js" />
   
Type.registerNamespace('WebPartExtenders');

WebPartExtenders.FullScreenExtender = function(element) {
    WebPartExtenders.FullScreenExtender.initializeBase(this, [element]);

    this._titleRow = null;
    this._fullScreenCell = null;
    this._webPartContainer = null;


    // Event handlers
    this._toggleFullScreenHandler = null;
    this._resizeFullScreenHandler = null;
}

WebPartExtenders.FullScreenExtender.prototype = {

    initialize: function() {
        WebPartExtenders.FullScreenExtender.callBaseMethod(this, 'initialize');
        var element = this.get_element();
        if (!element) throw Error.invalidOperation("Element required");
        this._toggleFullScreenHandler = Function.createDelegate(this, this._toggleFullScreen);
        this._resizeFullScreenHandler = Function.createDelegate(this, this._resizeFullScreen);

        this._webPartContainer = element.parentElement.parentElement.parentElement.parentElement;

        this._titleRow = this._webPartContainer.firstChild.firstChild.firstChild.firstChild.firstChild.firstChild;

        this.createFullScreenCell();

    },

    dispose: function() {

        // Remove the event handler from the element if attached.

        if (this._toggleFullScreen && this._fullScreenCell) {
            $removeHandler(this._fullScreenCell, 'click', this._toggleFullScreenHandler);
            this._toggleFullScreenHandlers = null;
        }
        if (this._resizeFullScreenHandler) {
            $removeHandler(window, 'resize', this._resizeFullScreenHandler);
            this._resizeFullScreenHandler = null;
        }


        WebPartExtenders.FullScreenExtender.callBaseMethod(this, 'dispose');
    },
    _toggleFullScreen: function() {
        if (this._webPartContainer.style.position != "absolute") {
            this._webPartContainer.style.position = "absolute";
            this._webPartContainer.style.top = "0px";
            this._webPartContainer.style.left = "0px";
            this._webPartContainer.style.width = document.body.clientWidth;
            this._webPartContainer.style.height = document.body.clientHeight;
            this.get_element().style.top = "0px";
            this.get_element().style.left = "0px";
            //TBD: get the actual width of the content area, so there is no scrollbar
            this.get_element().style.width = document.body.clientWidth - 15;
            this.get_element().style.height = document.body.clientHeight - 15;
        }
        else {
            this._webPartContainer.style.position = "";
            this._webPartContainer.style.top = "";
            this._webPartContainer.style.left = "";
            this._webPartContainer.style.width = "";
            this._webPartContainer.style.height = "";
            this.get_element().style.top = "";
            this.get_element().style.left = "";
            this.get_element().style.width = "";
            this.get_element().style.height = "";
        }
    },

    //TBD: refactor
    _resizeFullScreen: function() {
        if (this._webPartContainer.style.position == "absolute") {
            this._webPartContainer.style.position = "absolute";
            this._webPartContainer.style.top = "0px";
            this._webPartContainer.style.left = "0px";
            this._webPartContainer.style.width = document.body.clientWidth;
            this._webPartContainer.style.height = document.body.clientHeight;
            this.get_element().style.top = "0px";
            this.get_element().style.left = "0px";
            //TBD: get the actual width of the content area, so there is no scrollbar
            this.get_element().style.width = document.body.clientWidth - 15;
            this.get_element().style.height = document.body.clientHeight - 15;
        }
    },


    createFullScreenCell: function() {
        this._fullScreenCell = document.createElement('td');
        this._fullScreenCell.innerHTML = "&#049;";

        var attrClass = document.createAttribute('class');
        attrClass.value = 'FullScreenCell';
        this._fullScreenCell.attributes.setNamedItem(attrClass);


        $addHandler(this._fullScreenCell, "click", this._toggleFullScreenHandler);

        //$addHandler does not work with the window object
        //$addHandler(window, "resize", this._resizeFullScreenHandler);

        Sys.UI.DomEvent.addHandler(window, "resize", this._resizeFullScreenHandler);

        this._titleRow.appendChild(this._fullScreenCell);

    }
}
function alertSize()
{alert('test');}
WebPartExtenders.FullScreenExtender.registerClass('WebPartExtenders.FullScreenExtender', AjaxControlToolkit.BehaviorBase);   
